package com.cn.ryc.service;

import com.cn.ryc.mapper.StaffMapper;
import com.cn.ryc.model.Staff;
import com.cn.ryc.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StaffService {

    @Autowired
    private StaffMapper staffMapper;

    // 获取所有员工
    public ResponseEntity<ApiResponse<List<Staff>>> getAllStaff() {
        List<Staff> staffList = staffMapper.findAll();
        return ResponseEntity.ok(ApiResponse.success(staffList));
    }

    // 根据id获取员工
    public ResponseEntity<ApiResponse<Staff>> getStaffById(Long id) {
        Staff staff = staffMapper.findById(id);
        return ResponseEntity.ok(ApiResponse.success(staff));
    }

    // 添加员工
    public ResponseEntity<ApiResponse<String>> addStaff(Staff staff) {
        try {
            staffMapper.insertStaff(staff);
            return ResponseEntity.ok(ApiResponse.success("staff added successfully"));
        }catch (Exception e){
            return ResponseEntity.status(400).body(ApiResponse.error(400, "staff already exists"));
        }


    }

    // 更新员工
    public ResponseEntity<ApiResponse<String>> updateStaff(Staff staff) {
        staffMapper.updateStaff(staff);
        return ResponseEntity.ok(ApiResponse.success("更新成功"));
    }

    // 删除员工
    public ResponseEntity<ApiResponse<String>> deleteStaff(Long id) {
        try {
            staffMapper.deleteStaff(id);
            return ResponseEntity.ok(ApiResponse.success("Staff deleted successfully"));
        }
        catch (Exception e){
            return ResponseEntity.status(404).body(ApiResponse.error(404, "Staff not found"));
        }

    }
} 